package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.domain.Purchase;
import com.ruoyi.system.domain.vo.PurchaseVo;
import com.ruoyi.system.service.IGoodsService;
import com.ruoyi.system.service.IPurchaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Purchasedetails;
import com.ruoyi.system.service.IPurchasedetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单详情Controller
 * 
 * @author xy
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/system/purchasedetails")
public class PurchasedetailsController extends BaseController
{
    @Autowired
    private IPurchasedetailsService purchasedetailsService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IPurchaseService purchaseService;


    /**
     * 查询采购单详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchasedetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(Purchasedetails purchasedetails)
    {
        startPage();
        List<Purchasedetails> list = purchasedetailsService.selectPurchasedetailsList(purchasedetails);
        return getDataTable(list);
    }

    /**
     * 导出采购单详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchasedetails:export')")
    @Log(title = "采购单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Purchasedetails purchasedetails)
    {
        List<Purchasedetails> list = purchasedetailsService.selectPurchasedetailsList(purchasedetails);
        ExcelUtil<Purchasedetails> util = new ExcelUtil<Purchasedetails>(Purchasedetails.class);
        util.exportExcel(response, list, "采购单详情数据");
    }

    /**
     * 获取采购单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:query')")
    @GetMapping(value = "/{purchaseDetailsID}")
    public AjaxResult getInfo(@PathVariable("purchaseDetailsID") Long purchaseDetailsID)
    {
        return success(purchasedetailsService.selectPurchasedetailsByPurchaseDetailsID(purchaseDetailsID));
    }

    /**
     * 新增采购单详情
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:add')")
    @Log(title = "采购单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurchaseVo datas) {
        int k = 0;
        BigDecimal price = BigDecimal.valueOf(0);
        Long purchDetailsID = datas.getpurchaseDetailsID();
        Map<Long,Long> data = datas.getData();
        for(Long goodID:data.keySet()){
            System.out.println(goodID+"===>");
            Goods goods = goodsService.selectGoodsByGoodID(goodID);
            BigDecimal purchPrice = goods.getPurchPrice();  // 获取商品进价
            price = price.add(purchPrice.multiply(BigDecimal.valueOf(data.get(goodID))));
            goods.setNum(goods.getNum() + data.get(goodID));    // 加上商品数量
            goodsService.updateGoods(goods);    // 更新
            k = purchasedetailsService.insertPurchasedetails(new Purchasedetails(purchDetailsID,goodID,data.get(goodID)));
            System.out.println(k+"<===");
        }
        if (k > 0) {
            Purchase purchase = purchaseService.selectPurchaseByPurchaseID(purchDetailsID);
            System.out.println(purchase);
            purchase.setAllPrice(price);
            return toAjax(purchaseService.updatePurchase(purchase));
        }
        return toAjax(0);
    }

    /**
     * 修改采购单详情
     */
    @PreAuthorize("@ss.hasPermi('system:purchasedetails:edit')")
    @Log(title = "采购单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Purchasedetails purchasedetails)
    {
        return toAjax(purchasedetailsService.updatePurchasedetails(purchasedetails));
    }

    /**
     * 删除采购单详情
     */
    @PreAuthorize("@ss.hasPermi('system:purchasedetails:remove')")
    @Log(title = "采购单详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseDetailsIDs}")
    public AjaxResult remove(@PathVariable Long[] purchaseDetailsIDs)
    {
        return toAjax(purchasedetailsService.deletePurchasedetailsByPurchaseDetailsIDs(purchaseDetailsIDs));
    }
}
